/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.djdb.shop.dao;

import be.djdb.shop.Mailing;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolationException;

/**
* @author Lieven Roegiers
* @copyright 2013
* @from MY_SHOP
*/
public class MailingDAO {
    private final EntityManager em;
    public MailingDAO(EntityManager em) {
        this.em = em;
    }
    /**
     * @return
     * @throws DAOException
     */
    public List<Mailing> findAllMailings() throws DAOException {
        try{
            List<Mailing> results = em.createNamedQuery("Mailing.findAll", Mailing.class).getResultList();
            return results;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "product.cannot.findALLMailings.errormsg");
        }
        return null;
    }
    /**
     * @param Serialnr
     * @return
     * @throws DAOException
     */
    public Mailing findMailing(long Serialnr) throws DAOException{
        try{
            TypedQuery<Mailing> q = em.createNamedQuery("Mailing.findBySerialnr", Mailing.class);
            q.setParameter("serialnr", Serialnr);
            return q.getSingleResult();
        } catch (NoResultException ex) {
            Logger.getLogger(Mailing.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            return null;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "product.cannot.findMailing.errormsg");
        }
        return null;
    }
    
    public boolean isexist(String Serialnr) throws DAOException{
        try{ 
            TypedQuery<Long> q = em.createNamedQuery("Mailing.checkTitle", Long.class);
            q.setParameter("title",Serialnr);
            return q.getSingleResult() == 0;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "product.cannot.checkifexist.errormsg");
        }
        return false;
    }
    public void addMailing(Mailing p) throws DAOException{
        try {
            em.getTransaction().begin();
            em.persist(p);        
            em.getTransaction().commit();
        } catch (ConstraintViolationException | PersistenceException ex) {
            String message = ex.getMessage();
            Throwable nestedException = ex;
            while (nestedException != null) {
                if (nestedException instanceof ConstraintViolationException) {
                    //message = Util.buildErrorMessage((ConstraintViolationException)nestedException);
                    break;
                }
                nestedException = nestedException.getCause();
            }
            ERROR_Control(ex, "product.cannot.add.errormsg");
        }
    }
    public void updateMailing(Mailing g) throws DAOException{
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void removeMailing(long id) throws DAOException{
        try {
            em.getTransaction().begin();
            Mailing r = em.find(Mailing.class, id);
            if (r != null) {
                em.remove(r);
            }
            em.getTransaction().commit();
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "product.cannot.remove.errormsg");
        } 
    }
    public void removeMailing(Mailing m) throws DAOException{
        removeMailing(m.getId());
    }
    @Override
    protected void finalize() throws Throwable {
        if (em != null) {
            em.close();
        }
        super.finalize();
    }
    private void ERROR_Control(PersistenceException ex ,String msg) throws DAOException{
        //Todo translation
        throw new DAOException(msg + ex.getMessage(), ex, true);
    }
    private void ERROR_Control(RuntimeException ex ,String msg) throws DAOException{
        //Todo translation
        throw new DAOException(msg + ex.getMessage(), ex, true);
    }
    /*
    public boolean isIsReadyToSend() {
        return isReadyToSend;
    }
    public void setIsReadyToSend(boolean isReadyToSend) {
        this.isReadyToSend = isReadyToSend;
    }
*/
    
}
